import { Context, Next } from 'koa';

export const success = (code: number, message: string, data?: any) => {
  return { code, message, data };
};

export const message = success;

export const failure = async (ctx: Context, next: Next) => {
  try {
    return await next();
  } catch (err) {
    if (err.status === 401) {
      ctx.body = {
        code: err.status,
        message: err.originalError ? err.originalError.message : err.message
      };
    } else {
      throw err;
    }
  }
};